// pages/my/my.js
import Toast from '@vant/weapp/toast/toast';
var globalFn = require('../../utils/globalFn.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:app.globalData.userInfo,
    serviceUrl:app.globalData.serviceUrl,
    role:"",
    card:[
      {
        text:"待审核",
        num:1,
        status:0,
        active:1
      },
      {
        text:"待接单",
        num:1,
        status:2,
        active:3
      },
      {
        text:"已完成",
        num:1,
        status:6,
        active:5
      },
      {
        text:"驳回",
        num:1,
        status:1,
        active:2
      },
    ],
    repairList:[]
  },
  goto(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  goRepairList(e){
    wx.navigateTo({
      url: '/pages/repairList/repairList?active='+e.currentTarget.dataset.active,
    })
  },
  getRepairList(){
    let card = this.data.card;
    let that = this;
    wx.request({
      url: app.globalData.serviceUrl+'/repair/getRepairList',
      method:"POST",
      success:(res)=>{
        if(res.data.code==1){
          that.setData({
            repairList:res.data.data.filter(item=>item.userID===that.data.userInfo.id&&item.userType===app.globalData.storage.userType)
          })
          card.forEach(item=>{
            item.num = that.data.repairList.filter(it=>it.status===item.status).length;
          })
          that.setData({
            card:card
          })
        }
      },
      fail:(err)=>{
        console.log(err);
      }
    })
  },
  getData(){
    globalFn.myFn.getUserInfo();
    this.setData({
      userInfo:app.globalData.userInfo,
      serviceUrl:app.globalData.serviceUrl,
      role:app.globalData.userInfo.role==null?'学生':['教职工','宿管','后勤','维修工','普通管理员','超级管理员'][app.globalData.userInfo.role]
    })
    this.getRepairList();
    wx.stopPullDownRefresh();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let toast = Toast.loading({
      mask: false,
      message: '加载中...'
    })
    this.getData();
    toast.clear();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})